package com.twu.biblioteca.menu;

import com.twu.biblioteca.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Reserve implements MenuOption {

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Reserve reserve = (Reserve) o;

        if (reserveOption != null ? !reserveOption.equals(reserve.reserveOption) : reserve.reserveOption != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        return reserveOption != null ? reserveOption.hashCode() : 0;
    }

    private static Reserve reserveOption;

    public static Reserve getReserveOption() {
        if (reserveOption == null) {
            reserveOption = new Reserve();
        }
        return reserveOption;
    }

    @Override
    public void execute() {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        LibraryManager libraryManager = LibraryManager.getInstance();
        if (libraryManager.isLoggedIn()) {
            try {
                libraryManager.printToScreen(Messages.REQUEST_INPUT_MESSAGE);
                libraryManager.reserveBook(reader.readLine());
                libraryManager.printToScreen(libraryManager.getOutput());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            libraryManager.printToScreen(Messages.ERROR_NOT_LOGGED_IN);
        }
    }
}
